package com.j.lemon.learn.basic.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;


public class MyServlet implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        String a = servletConfig.getInitParameter("qqq");
        System.out.println("servlet 初始化参数 qqq :"+a);
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void  service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        long startTime = System.currentTimeMillis();
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;

        String name = httpServletRequest.getParameter("name");
        byte [] b = name.getBytes();
        for(byte bb : b){
//            System.out.println(bb);
        }
//        System.out.println();
//        System.out.println(name);

        Enumeration enumeration=httpServletRequest.getHeaderNames();
        while(enumeration.hasMoreElements()){
            String headerName=enumeration.nextElement().toString();
//            System.out.println(headerName+":"+httpServletRequest.getHeader(headerName));
        }
//        System.out.println("请求:"+httpServletRequest.getAuthType());
//        System.out.println("请求方式:"+httpServletRequest.getMethod());
//        System.out.println("请求协议种类:"+httpServletRequest.getProtocol());
//        System.out.println("请求资源路径:"+httpServletRequest.getRequestURI());
//        System.out.println("请求的路径信息:"+httpServletRequest.getRequestURL());
//        System.out.println("请求servlet路径:"+httpServletRequest.getServletPath());

        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        httpServletResponse.setContentType("text/html;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();

        PrintWriter printWriter = httpServletResponse.getWriter();
        printWriter.println("哈哈哈");
        printWriter.flush();
        printWriter.close();
        long endTime = System.currentTimeMillis();
        System.out.println("cost:"+(endTime-startTime));
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("容器销毁");
    }
}
